#include "contextviewer.h"
#include "qshortcut.h"
#include "qdeclarativeengine.h"

ContextViewer::ContextViewer(QWidget *parent)
    : QDeclarativeView(parent)
{
    this->setAttribute(Qt::WA_TranslucentBackground);
    this->setStyleSheet("background:transparent;");
    sWflags="";
    posFixed=false;
    sizeFixed=false;
    hideWhenUnfocus=false;
    QObject* obj = (QObject*)this->engine();
    connect(obj,SIGNAL(quit()),QCoreApplication::instance(),SLOT(quit()));
    QShortcut * reload = new QShortcut(QKeySequence(Qt::Key_F5), this);
    QObject::connect(reload, SIGNAL(activated()), this, SLOT(reloadFile()));
}


ContextViewer::~ContextViewer()
{

}

void ContextViewer::setWinFlags(QString sWinF){
    this->sWflags=sWinF;
    QStringList listFlags= sWflags.split(",");
    foreach (QString s, listFlags){
        if (s == "X11BypassWindowManagerHint"){
            qDebug()<<"X11BypassWindowManagerHint";
            this->setWindowFlags(Qt::X11BypassWindowManagerHint);
        }else if (s == "SplashScreen"){
            qDebug()<<"SplashScreen";
            this->setWindowFlags(Qt::SplashScreen);
        }else if (s == "FramelessWindowHint"){
            qDebug()<<"FramelessWindowHint";
            this->setWindowFlags(Qt::FramelessWindowHint);
        }else if (s == "WindowStaysOnTopHint"){
            qDebug()<<"WindowStaysOnTopHint";
            this->setWindowFlags(Qt::WindowStaysOnTopHint);
        }
    }

}
void ContextViewer::reloadFile(){
     qDebug()<<"reload ... ";
     this->engine()->clearComponentCache();
     qDebug()<<"(re)load file "<< lastFileLoaded;
     this->setSource(QUrl::fromLocalFile(lastFileLoaded));
     this->rootContext()->setContextProperty("qmlviewer", this);
     this->rootContext()->setContextProperty("stdin", m_reader);
     this->setResizeMode(QDeclarativeView::SizeViewToRootObject);
}

void ContextViewer::loadFile(QString qmlFile){
    if(qmlFile == this->lastFileLoaded){
        qDebug()<<"loaded yet";
        return;
    }
    this->lastFileLoaded=qmlFile;
    reloadFile();
}

void ContextViewer::setReader(StdInReader *reader){
    this->m_reader=reader;
    QObject::connect(m_reader,SIGNAL(onHide()),this,SLOT(hide()));
    QObject::connect(m_reader,SIGNAL(onShow()),this,SLOT(onShow()));
    QObject::connect(m_reader,SIGNAL(onTitle(QString)),this,SLOT(setWindowTitle(QString)));
    QObject::connect(m_reader,SIGNAL(onLoadFile(QString)),this,SLOT(loadFile(QString)));
    QObject::connect(m_reader,SIGNAL(onLoadPosAndSize(QString,QString)),this,SLOT(fixePosAndSize(QString,QString)));
}

void ContextViewer::onShow(){
    qDebug()<<"before Show";
    this->show();
    qDebug()<<"After Show";

    //this->setWindowModality(Qt::ApplicationModal);
    //this->activateWindow();
    //this->raise();
    //this->setFocus();
    this->grabKeyboard();
}

void ContextViewer::hideEvent(QHideEvent *e){
    qDebug()<<"hideEvent";

    //this->releaseKeyboard();
}
void ContextViewer::focusOutEvent ( QFocusEvent * event ){
     qDebug()<<"focus lost";
     emit(focusOut());
     if(hideWhenUnfocus){
         qDebug()<<"hideWhenUnfocus ==> true";
         this->hide();
     }
}

void  ContextViewer::fixePosAndSize(QString sPos,QString sSize){
    this->fixePosition(sPos);
    this->fixeSize(sSize);
}

void ContextViewer::fixePosition(QString sPos){
    this->sPos=sPos;
    qDebug()<<"sPos"<<sPos;
    this->posFixed=true;
}

void ContextViewer::fixeSize(QString psSize){
    sSize=psSize;
    qDebug()<<"sSize"<<sSize;
    this->sizeFixed=true;
}

void ContextViewer::setHideWhenUnfocus(bool hide){
    this->hideWhenUnfocus=hide;
}

void ContextViewer::showEvent ( QShowEvent * e){
    qDebug()<<"showEvent == START";
    //this->QDeclarativeView::showEvent(e);
    if(sizeFixed){
        QStringList geoL = sSize.split("x");
        int iW = geoL.at(0).toInt();
        int h = geoL.at(1).toInt();
        qDebug()<<"w:"<<iW<<", h:"<<h;
        this->resize(iW,h);
    }

    if(posFixed){
        QStringList posL = sPos.split(",");
        int x = posL.at(0).toInt();
        int y = posL.at(1).toInt();
        qDebug()<<"x:"<<x<<", y:"<<y;
        this->move(x,y);
    }else{
        QRect rect = QApplication::desktop()->rect();
        this->move (rect.center() - this->rect().center());
    }

    qDebug()<<"showEvent == END";
    //this->QDeclarativeView::showEvent(e);
}
